package com.amap.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.amap.R;
import com.amap.controller.ApplicationController;
import com.amap.model.Place;
import com.amap.overlay.HelloItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GMapActivity extends MapActivity {

	//
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
				
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
		Drawable userLoc = this.getResources().getDrawable(R.drawable.marker2);
		HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
		HelloItemizedOverlay userOverlay = new HelloItemizedOverlay(userLoc, this);
		
		//Location loc = new Location(Double.parseDouble("-8.051411"), Double.parseDouble("-34.950385"));
		
		String coordinates[] = {"-8.051411", "-34.950385"};
		double lat = Double.parseDouble(coordinates[0]);
		double lng = Double.parseDouble(coordinates[1]);

		GeoPoint p = new GeoPoint(
		    (int) (lat * 1E6), 
		    (int) (lng * 1E6));

		mapView.getController().animateTo(p);
		mapView.getController().setZoom(21); 
		
		OverlayItem userItem = new OverlayItem(p, "User Location", "Long: \n Lat: ");
		
		userOverlay.addOverlay(userItem);
		
		
		Intent intent = getIntent();    
        String idPlace = intent.getStringExtra("id");
        
        Place placeAux = ApplicationController.getInstance().getPlaceById(Integer.parseInt(idPlace));
		
		ArrayList<Place> places = new ArrayList<Place>();//ApplicationController.getInstance().getLocations(loc ,"5000", "food", "carne");
		places.add(placeAux);

		GeoPoint point;
		ArrayList<GeoPoint> geo = new ArrayList<GeoPoint>();
		geo.add(p);
		

		for (int i = 0; i < places.size(); i++) {
			point = new GeoPoint((int) (places.get(i).getLocation().getLatitude() * 1e6), (int) (places.get(i).getLocation().getLongitude() * 1e6));
			geo.add(point);
			OverlayItem overlayitem = new OverlayItem(point, places.get(i).getName(), places.get(i).getAdress());
			itemizedoverlay.addOverlay(overlayitem);

		}
		
		zoomInBounds(mapView, geo);		
		mapOverlays.add(itemizedoverlay);
		mapOverlays.add(userOverlay);
		
	}
	
	
	private static void zoomInBounds(final MapView view, final ArrayList<GeoPoint> bounds) {

	    int minLat = Integer.MAX_VALUE;
	    int minLong = Integer.MAX_VALUE;
	    int maxLat = Integer.MIN_VALUE;
	    int maxLong = Integer.MIN_VALUE;

	    for (GeoPoint point : bounds) {
	        minLat = Math.min(point.getLatitudeE6(), minLat);
	        minLong = Math.min(point.getLongitudeE6(), minLong);
	        maxLat = Math.max(point.getLatitudeE6(), maxLat);
	        maxLong = Math.max(point.getLongitudeE6(), maxLong);
	    }

	    final MapController controller = view.getController();
	    controller.zoomToSpan(Math.abs(minLat - maxLat), Math.abs(minLong - maxLong));
	    controller.animateTo(new GeoPoint((maxLat + minLat) / 2,
	        (maxLong + minLong) / 2));
	}
	
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
}